package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.AddressBookDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.PackagePrivate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: AddressBookController
 *
 * @AUTHOR: zhang
 * DATE: 2023/12/1
 * DESCRIPTION :
 */
@Slf4j
@Api(tags = "C端-地址信息表现层接口") //@Api:用户描述类的相关信息
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;

    @ApiOperation("查询当前用户的所有的地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> selectAllAddressBook(){
        log.info("【查询地址列表】");
        return addressBookService.selectAllAddressBook();
    }

    @ApiOperation("新增地址")
    @PostMapping
    public Result inseertAddressBook(@RequestBody @Valid AddressBookDTO addressBookDTO){
        log.info("【新增地址】要新增的地址信息:{}",addressBookDTO);
        addressBookService.inseertAddressBook(addressBookDTO);
        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public void setDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("【设置默认地址】设置的默认地址信息:{}",addressBook);
        addressBookService.update(addressBook);
    }


    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> selectAddressBookById(@PathVariable Long id){
        log.info("【根据id查询地址信息】{}",id);
        AddressBook addressBook = addressBookService.selectAddressBookById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result updateAddressBookById(@RequestBody AddressBookDTO addressBookDTO){
        log.info("【根据id修改地址信息】{}",addressBookDTO);
        addressBookService.updateAddressBook(addressBookDTO);
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result deleteAddressBookById(Long id){
        log.info("【根据id删除地址信息】{}",id);
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        log.info("【查询默认地址】");
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        return Result.success(addressBook);
    }
}
